package de.bbl.android;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import de.bbl.android.fragment.TabFragment;
import de.bbl.android.tools.Tools;

public class BBLAndroidFragmentActivity extends FragmentActivity {

	public static String sd_path = ""; 
	public static String bbl_sd_path = "" ; 
	public static String bbl_temp_sd_path = ""; 
	public static String bbl_temp_img_sd_path = "";
	public static String currentFragmentTag = "";
	private TabFragment currentFragment = null;
	ArrayList<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>();
	
	public static final int DATA_GAMEDAY_ACTUAL = 0;
	public static final int DATA_GAMEDAY_LAST = 1;
	public static final int DATA_GAMEDAY_NEXT = 2;
	public static final int DATA_MAGAZIN = 3;
	public static final int DATA_STANDINGS = 4;
	public static final int DATA_ALLMATCHES = 5;
	public static final int DATA_MATCHDAY_NUMBER = 6;
	public static final int DATA_MATCHDAYS = 7;
	public static final int DATA_PLAYOFF_PICTURE = 8;
	public static final int DATA_STATS_POINTS = 9;
	public static final int DATA_STATS_REBOUNDS = 10;
	public static final int DATA_STATS_ASSISTS = 11;
	public static final int DATA_STATS_BLOCKS = 12;
	public static final int DATA_VIDEOS = 13;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // initializing needed Vars
        sd_path = Environment.getExternalStorageDirectory().getPath();
        bbl_sd_path = sd_path + getString(R.string.bbl_path);
        bbl_temp_sd_path = sd_path + getString(R.string.temp_path);
        bbl_temp_img_sd_path = sd_path + getString(R.string.img_path);

        // removes Titlebar to set own later
        if(android.os.Build.VERSION.SDK_INT < 11) { 
            requestWindowFeature(Window.FEATURE_NO_TITLE); 
        }  
        
        // sets the main content view
        setContentView(R.layout.activity_bblandroid_fragment);

        // set own title bar (similar title bar like the action bar since SDK > 10)
        if(android.os.Build.VERSION.SDK_INT < 11) { 
            LinearLayout lin = (LinearLayout) findViewById(R.id.title_bblandroid);
            lin.setVisibility(LinearLayout.VISIBLE);
        }  
        
        // preparing Sdcard
        checkSD();
        
        try {
	        // Grab the instance of TabFragment that was included with the layout and have it
	        // launch the initial tab.
	        FragmentManager fm = getSupportFragmentManager();
	        TabFragment tabFragment = (TabFragment) fm.findFragmentById(R.id.fragment_tab);
	        tabFragment.startCenterRepresentation();
	        currentFragment = tabFragment;

        } catch (Exception e) {
        	Tools.AppendToErrorFile(e.getMessage());
        }
    }
	
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	getMenuInflater().inflate(R.menu.activity_bblmain_dummy, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}

    public void TabFragmentRefreshOnClick(View view) {
    	currentFragment.RefreshData();
    }

    public void TabFragmentOptionsMenuOnClick(View view) {
    	currentFragment.OptionsMenu();
    	// currentFragment.OptionsMenu();
    }

    private void checkSD() {
    	if (Tools.isSdPresent()) {
	    	boolean test = (new File(bbl_sd_path).exists());
	    	if (! test) {
	    		File f = new File(bbl_sd_path);
	    		f.mkdirs();
	    	}
	    	
	    	test = (new File(bbl_temp_sd_path).exists());
	    	if (! test) {
	    		File f = new File(bbl_temp_sd_path);
	    		f.mkdirs();
	    	}

	    	test = (new File(bbl_temp_img_sd_path).exists());
	    	if (! test) {
	    		File f = new File(bbl_temp_img_sd_path);
	    		f.mkdirs();
	    	}
    	}
	}

	@Override
	public void onBackPressed() {
	    String tag = getActiveFragments();
	    if (tag == "" || currentFragment.getCurrentTab() == 0x1) {
    		finish();
	    	// System.exit(0);
	    }
	    else {
	    	if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
	    		super.onBackPressed();
	    		tag = getActiveFragments();
	    		if (tag != "") {
	    			currentFragment.setCurrentTab(tag.toString());
	    		}
	    	} else
	    		currentFragment.setGameCenter();
	    }
	}
	
	@Override
	public void onAttachFragment (Fragment fragment) {
	    fragList.add(new WeakReference<Fragment>(fragment));
	}

	public String getActiveFragments() {
		String retVal = "";
	    for(WeakReference<Fragment> ref : fragList) {
	        Fragment f = ref.get();
	        if(f != null) {
	            if(f.isVisible()) {
	            	if (f.getTag() != null) {
	            		if (! f.getTag().contains("extended"))
	            			retVal = f.getTag();
	            	}
	            }
	        }
	    }
	    return retVal;
	}
}
